import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-05-16
 * Time: 18:37
 */
public class new1 extends JFrame{
    private HashMap<String,String> hashMap=new HashMap<>();

    // 构造方法和组件添加

    public new1() {
        // ...（设置标题、大小、默认关闭操作、布局等）
        setTitle("用户登录");
        setSize(400, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        // 用户名输入
        JLabel usernameLabel = new JLabel("用户名:");
        JTextField usernameField = new JTextField(28);
        add(usernameLabel);
        add(usernameField);

        // 密码输入
        JLabel passwordLabel = new JLabel("密码:");
        JPasswordField passwordField = new JPasswordField(28);
        add(passwordLabel);
        add(passwordField);

        // 登录按钮
        JButton loginButton = new JButton("登录");
        add(loginButton);

        JButton SignButton = new JButton("注册");
        add(SignButton);

        // 为登录按钮添加事件监听器
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());

                // 这里应该从数据库或其他存储中验证用户名和密码
                if (isValidUser(username, password)) {
                    JOptionPane.showMessageDialog(null, "登录成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "用户名或密码错误！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        SignButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                if (!isUsername(username)) {
                    JOptionPane.showMessageDialog(null, "注册成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "用户已存在", "错误", JOptionPane.ERROR_MESSAGE);
                }
                hashMap.put(username,password);
            }
        });
        // 设置界面可见
        setVisible(true);
    }

    // 模拟验证用户名和密码的方法
    private boolean isUsername(String username) {
        // 这里应该与存储的用户数据进行比较
        // 仅为示例，这里直接返回true
        return hashMap.containsKey(username);
    }
    private boolean  isValidUser(String username, String password) {
        // 这里应该与存储的用户数据进行比较
        // 仅为示例，这里直接返回true
        String tmp="";
        if (hashMap.containsKey(username)){
            tmp=hashMap.get(username);
        }else {
            return false;
        }
        if (tmp.equals(password)){
            return true;
        }
        return false;
    }

    // main方法与RegistrationForm类似
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new new1();
            }
        });
    }
}
